#include "SoundPlayer.h"
#include "SoundDef.h"
#include "SimpleAudioEngine.h"
#include "Game.h"

#define CC_SND_ENGINE CocosDenshion::SimpleAudioEngine::sharedEngine()
namespace LTGame{
SoundPlayer* SoundPlayer::instance = new SoundPlayer;

SoundPlayer::SoundPlayer()
	:effSoundId(0),curEffSoundPath("")
{
	//soundsMap = new std::map<std::string, Sound*>();
}

SoundPlayer::~SoundPlayer()
{
	//CC_SAFE_DELETE(soundsMap);
}

void SoundPlayer::release()
{
	stopBk();
	stopEff();

	//CC_SAFE_DELETE(instance);
}

void SoundPlayer::playBk(std::string sndName)
{
	CC_SND_ENGINE->playBackgroundMusic(std::string(SND_DIR+sndName).c_str(),  true);
}

void SoundPlayer::playBkMain(std::string sndName)
{
	if ( CCGame->cghas)
	{
		CCGame->cghas =false;
		CC_SND_ENGINE->playBackgroundMusic(std::string(SND_DIR+sndName).c_str(),  true);
	}
	else if (CC_SND_ENGINE->isBackgroundMusicPlaying())
	{

	}else CC_SND_ENGINE->playBackgroundMusic(std::string(SND_DIR+sndName).c_str(),  true);
		
}

void SoundPlayer::pauseBk()
{
	CC_SND_ENGINE->pauseBackgroundMusic();
}

void SoundPlayer::resumeBk()
{
	CC_SND_ENGINE->resumeBackgroundMusic();
}

void SoundPlayer::stopBk()
{
	CC_SND_ENGINE->stopBackgroundMusic(true);
}

void SoundPlayer::rewindBk()
{
	CC_SND_ENGINE->rewindBackgroundMusic();
}

void SoundPlayer::playEff(std::string sndName)
{
	curEffSoundPath = SND_DIR + sndName;
	effSoundId = CC_SND_ENGINE->playEffect(curEffSoundPath.c_str());
}

void SoundPlayer::playEffqing(std::string sndName)
{
	curEffSoundPath = SND_DIR + sndName;
	CCGame->effsoundqing = CC_SND_ENGINE->playEffect(curEffSoundPath.c_str(),false);
}

void SoundPlayer::playEffwuqi1(std::string sndName)
{
	curEffSoundPath = SND_DIR + sndName;
	effSoundId1 = CC_SND_ENGINE->playEffect(curEffSoundPath.c_str(),true);
}
void SoundPlayer::playEffwuqi2(std::string sndName)
{
	curEffSoundPath = SND_DIR + sndName;
	effSoundId2 = CC_SND_ENGINE->playEffect(curEffSoundPath.c_str(),true);
}
void SoundPlayer::playEffwuqi3(std::string sndName)
{
	curEffSoundPath = SND_DIR + sndName;
	effSoundId3 = CC_SND_ENGINE->playEffect(curEffSoundPath.c_str(),true);
}

void SoundPlayer::pauseEff()
{
	switch(CCGame->prowuqisound){
	case 0:
		CC_SND_ENGINE->stopEffect(effSoundId1);
		break;
	case 1:
		CC_SND_ENGINE->stopEffect(effSoundId2);
		break;
	case 2:
		CC_SND_ENGINE->stopEffect(effSoundId3);
		break;
	default:
		//CC_SND_ENGINE->pauseEffect(effSoundId);
		break;
	}
	
}

void SoundPlayer::pauseLoadEff()
{
	switch(CCGame->wuqisound){
	case 0:
		CC_SND_ENGINE->stopEffect(effSoundId1);
		break;
	case 1:
		CC_SND_ENGINE->stopEffect(effSoundId2);
		break;
	case 2:
		CC_SND_ENGINE->stopEffect(effSoundId3);
		break;
	default:
		//CC_SND_ENGINE->pauseEffect(effSoundId);
		break;
	}

}

void SoundPlayer::playWuQiEff()
{
	switch(CCGame->wuqisound){
	case 0:
		//CC_SND_ENGINE->resumeEffect(effSoundId1);
		SoundPlayer::getInstance()->playEffwuqi1(SND_ZIDAN1);
		break;
	case 1:
		//CC_SND_ENGINE->resumeEffect(effSoundId1);
		SoundPlayer::getInstance()->playEffwuqi2(SND_ZIDAN2);
		break;
	case 2:
		//CC_SND_ENGINE->resumeEffect(effSoundId1);
		SoundPlayer::getInstance()->playEffwuqi3(SND_ZIDAN3);
		break;
	default:
		//CC_SND_ENGINE->pauseEffect(effSoundId);
		break;
	}

}
void SoundPlayer::resumeEff()
{
	CC_SND_ENGINE->resumeEffect(effSoundId);
}

void SoundPlayer::stopEff()
{
	do 
	{
		//CC_BREAK_IF(effSoundId == 0);

		//CC_ASSERT(!curEffSoundPath.empty());

		CC_SND_ENGINE->stopEffect(effSoundId);
		CC_SND_ENGINE->unloadEffect(curEffSoundPath.c_str());
		effSoundId = 0;
		effSoundId1=0;
			effSoundId2=0;
			effSoundId3=0;
		curEffSoundPath.clear();

	} while (0);
}

void SoundPlayer::rewindEff()
{

}
/*void SoundPlayer::playSeverSound(byte type)
{
	
}*/
}